package org.redxentertainment.videomanager;

import java.util.ArrayList;

import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Color;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class VideoAdapter extends ArrayAdapter<Video> {
	
	public ArrayList<Video> 	videos;
	
	public VideoAdapter(Context context, int textViewResourceId, ArrayList<Video> videos) {
        super(context, textViewResourceId, videos);
        this.videos = videos;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
	        View 			view = convertView;
	        ImageView 		thumbView, newIcon;
	        String			description;
	        TextView 		title, desc;
	        VideoDatabase 	videoDB = new VideoDatabase( getContext() );

	        
	        if (view == null) {
	        	Context context = getContext();
	            LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	            view = vi.inflate(R.layout.video, null);
	        }
	        
	        title 	= (TextView) view.findViewById(R.id.title);
            desc 	= (TextView) view.findViewById(R.id.description);
	        
	        Video video = videos.get(position);
	        
	        if(video.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            	title.setTextSize(22);
            	desc.setTextSize(18);
            } else {
            	title.setTextSize(16);
            	desc.setTextSize(14);
            }
	        
	        if (video != null) {
	        
		        if(video.album != "null") {
	        		description = video.album;
	        	} else if(video.artist != "null") {
	        		description = video.artist;
	        	} else {
	        		description = "";
	        	}
		        
                title.setText(video.title);
                desc.setText(description);
	        
		        thumbView = (ImageView )view.findViewById(R.id.icon);
		        if(video.thumb != null) {
		        	thumbView.setImageBitmap(video.thumb);
		        } else {
		        	thumbView.setImageResource(R.drawable.missing_big);
		        	//thumbView.setBackgroundColor(Color.BLACK);
		        	//desc.setText("POSSIBLE BAD VIDEO!");
		        }
		        
		        newIcon = (ImageView )view.findViewById(R.id.newicon);
		        newIcon.setImageResource(R.drawable.newvid);
		        newIcon.setVisibility(newIcon.VISIBLE);
		        
		        if(video.isQueued) {
		        	newIcon.setImageResource(R.drawable.queue);
		        } else if(video.wasWatched) {
		        	newIcon.setVisibility(newIcon.INVISIBLE);
		        }
	        }
	        
	        videoDB.close();
	        return view;
	}
}
